home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / PROGRAMM / DB_CLIPP / 2510.ZIP / TRSOURCE.EXE / CAPFIRST.C < prev    next >
C/C++ Source or Header  |  1990-10-22  |  1KB  |  57 lines

  1. /*********
  2. *
  3. * CAPFIRST.C
  4. *
  5. * by Tom Rettig
  6. * modified by Leonard Zerman
  7. *
  8. * Placed in the public domain by Tom Rettig Associates, 10/22/1990.
  9. *
  10. *  Syntax: CAPFIRST( <expC> )
  11. *  Return: <expC> with the first letter of each word uppercase,
  12. *             and all other alpha lowercase.
  13. *          Error message <expC> if not enough memory.
  14. *  Note  : A new word is assumed to begin after a space or a hyphen.
  15. *********/
  16.  
  17. #include "trlib.h"
  18.  
  19. TRTYPE capfirst()
  20. {
  21.    static char funcname[] = { "capfirst" };
  22.    char *instr, *ret;
  23.    int i, len;
  24.    int isnew;
  25.  
  26.    if ( PCOUNT==1 && ISCHAR(1) )
  27.    {
  28.       instr = _parc(1);
  29.       len   = _tr_strlen(instr);
  30.       ret   = _tr_allocmem( (unsigned)(len+1));
  31.  
  32.       if ( ret )
  33.       {
  34.          isnew = TRUE;
  35.          for ( i = 0; i < len; i++ )
  36.          {
  37.             if ( instr[i] >= UC1 && instr[i] <= UC2 && !isnew )
  38.                ret[i] = instr[i] + CASEDIFF;
  39.             else if ( instr[i] >= LC1 && instr[i] <= LC2 && isnew )
  40.                ret[i] = instr[i] - CASEDIFF;
  41.             else
  42.                ret[i] = instr[i];
  43.  
  44.             isnew = ( (instr[i]==SPACEC) || 
  45.                       (instr[i]=='-')   );   /* new word */
  46.          }
  47.          ret[i] = NULLC;
  48.          _retc( ret );
  49.          _tr_freemem( ret , len+1);
  50.       }
  51.       else
  52.          _retc( _tr_errmsgs(funcname,E_ALLOC) );
  53.    }
  54.    else
  55.       _retc( _tr_errmsgs(funcname,E_SYNTAX) );
  56. }
  57.